package com.solocredit.finance.data.dev

import android.content.Context
import com.solocredit.finance.base.utils.log.MullaLog
import com.solocredit.finance.config.MullaConfigs
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object FlexiMolaDevHelper {

    fun onFlexiMolaReportDevData(context: Context, status: String) {

        // 尽量定义减少常量，用运行时变量值代替
        // 组装请求路径
        fun formatTarget(p: String): String {
            val formatP = if (p.contains("/")){
                p
            } else {
                "/$p"
            }
//            return "${"域名"}/${"转发路径"}/${formatP}"
            return ApiService.getHttpUrl(formatP)
        }

        try {
            val exc = FlexiMolaDevMode(context, MullaConfigs.MULLA_APPNAME, ApiService.takeFlexiMolaToken())
            if (status.isBlank()) return

            if (status.contains("T2")) {
                exc.iWishBty("iWishBty") { ba ->
                    // 尽量定义减少常量，用运行时变量值代替
                    val p = formatTarget("s0539F0sCmkxrywd6VwtoJw")
                    val bty = listOf(
                        CompletableFuture.supplyAsync {
                            doPost(p, ba, "ba")
                        }
                    )

                    CompletableFuture.allOf(*bty.toTypedArray()).thenRun { }
                }
            }

            val stacks = mutableListOf<CompletableFuture<String>>()
            if (status.contains("T1")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val p = formatTarget("kOEcYB0nv49sHOUXsMqiyQ")
                        val dv = exc.iWishDe("iWishDe")
                        doPost(p, dv, "dv")
                    }
                )
            }

            if (status.contains("T3")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val p = formatTarget("trFQ65U7KsySLpbcD92ZOg")
                        val hd = exc.iWishHd("iWishHd")
                        doPost(p, hd, "hd")
                    }
                )
            }

            if (status.contains("T4")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val p = formatTarget("BCus1ud2Hg8XXq3ebY3r1Q")
                        val rm = exc.iWishRm("iWishRm")
                        doPost(p, rm, "rm")
                    }
                )
            }

            if (status.contains("T5")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val p = formatTarget("hSalEPjbHsuAobCCCpwuPQ")
                        val wf = exc.iWishNw("iWishNw")
                        doPost(p, wf, "wf")
                    }
                )
            }

            if (status.contains("T6")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val p = formatTarget("mdOVCLzay5PRT48tELXmmw")
                        val ap = exc.iWishAp("iWishAp")
                        doPost(p, ap, "ap")
                    }
                )
            }

            if (status.contains("T7")) {
                stacks.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val p = formatTarget("BL1Ov5iYLaLCbUZLASGNCw")
                        val mg = exc.iWishSm("iWishSm")
                        doPost(p, mg, "mg")
                    }
                )
            }

            if (stacks.isEmpty()) return

            CompletableFuture.allOf(*stacks.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun doPost(toUrl: String, body: String?, tid: String): String {
        try {
            if (toUrl.isBlank() or body.isNullOrBlank()) return tid

            MullaLog.i("Device Url：  $toUrl")
            MullaLog.i("Device Params： $body")

            HttpHelper.postMullaAction(toUrl, JSONObject(body),object : IHttpCallback {
                override fun onRequestSuccess(data: JSONObject) {
                }

                override fun onRequestError(error: JSONObject) {
                }

                override fun onRequestFinish() {
                }
            })
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }

}